home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / slix0987.zip / VGADM10.ZIP / GFX2.CPP < prev    next >
C/C++ Source or Header  |  1995-01-22  |  19KB  |  470 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //                                                                         //
  3. // GFX2.CPP - VGA Trainer Program secondary module containing graphics     //
  4. //            functions.  Note: This module does not follow a lot of good  //
  5. //            programming practices.  It was built to be used with the     //
  6. //            VGA tutorial series.  If you are planning on using this      //
  7. //            module with a different source file, some modifications may  //
  8. //            be necessary.                                                //
  9. //                                                                         //
  10. // Author        : Grant Smith (Denthor) - denthor@beastie.cs.und.ac.za    //
  11. // Translator    : Christopher G. Mann   - r3cgm@dax.cc.uakron.edu         //
  12. //                                                                         //
  13. // Last Modified : January 21, 1995                                        //
  14. //                                                                         //
  15. /////////////////////////////////////////////////////////////////////////////
  16.  
  17. //               //
  18. // INCLUDE FILES //
  19. //               //
  20.  
  21.   #include <alloc.h>
  22.                            // farcalloc(), farfree()
  23.   #include <dos.h>
  24.                            // geninterrupt(), FP_SEG
  25.  
  26. //         //
  27. // DEFINES //
  28. //         //
  29.  
  30.   #if !defined(PI)
  31.     #define PI 3.1415927
  32.   #endif
  33.  
  34.   #if !defined(VGA)
  35.     #define VGA 0xA000
  36.   #endif
  37.  
  38. //          //
  39. // TYPEDEFS //
  40. //          //
  41.  
  42.   typedef unsigned char byte;
  43.   typedef unsigned int  word;
  44.  
  45. //                     //
  46. // FUNCTION PROTOTYPES //
  47. //                     //
  48.  
  49.   // SCREEN FUNCTIONS
  50.   void  SetUpVirtual(byte far *&Virscr, word &Vaddr);
  51.   void  ShutDown    (byte far *&Virscr);
  52.   void  Cls         (byte Col, word Where);
  53.   void  Flip        (word source, word dest);
  54.   void  WaitRetrace ();
  55.  
  56.   // MODE SETTING FUNCTIONS
  57.   void  SetMCGA     ();
  58.   void  SetText     ();
  59.  
  60.   // PALLETTE CLASS (DATA OBJECT AND RELATED FUNCTIONS)
  61.   class Pal {
  62.     public:
  63.       Pal();
  64.       void PalSet   (byte Rset, byte Gset, byte Bset);
  65.       void PalGet   (byte Col);
  66.       void PalPut   (byte Col);
  67.       void PalInc   ();
  68.       void PalDec   ();
  69.     private:
  70.       byte R;   // 0-63
  71.       byte G;   // 0-63
  72.       byte B;   // 0-63
  73.   };
  74.  
  75.   // MATH-LIKE FUNCTIONS
  76.   float rad         (float theta);
  77.   int   sgn         (int a);
  78.  
  79.   template<class T>
  80.   T abso(T value) {  if (value >= 0) return value;  else return -value; }
  81.  
  82.   // DRAWING FUNCTIONS
  83.   void  Putpixel    (word X, word Y, byte Col, word Where);
  84.   void  PutpixelVGA (word X, word Y, byte Col);
  85.   void  Line        (int a, int b, int c, int  d, int col, word Where);
  86.   void  Hline       (word X1, word X2, word Y, byte Col, word Where);
  87.  
  88.  
  89. //-------------------------VIRTUAL SCREEN FUNCTIONS------------------------//
  90.  
  91. /////////////////////////////////////////////////////////////////////////////
  92. //                                                                         //
  93. // SetUpVirtual() - This sets up the memory needed for a virtual screen.   //
  94. //                                                                         //
  95. /////////////////////////////////////////////////////////////////////////////
  96.  
  97. void SetUpVirtual(byte far *&Virscr, word &Vaddr) {
  98.   Virscr = (byte far *) farcalloc(64000,1);
  99.   Vaddr = FP_SEG(Virscr);
  100. }
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103. //                                                                         //
  104. // ShutDown() - This frees the memory used by a virtual screen.            //
  105. //                                                                         //
  106. /////////////////////////////////////////////////////////////////////////////
  107.  
  108. void ShutDown(byte far *&Virscr) {
  109.   farfree(Virscr);
  110. }
  111.  
  112. /////////////////////////////////////////////////////////////////////////////
  113. //                                                                         //
  114. // Cls() - This clears the screen at Where to color Col.                   //
  115. //                                                                         //
  116. /////////////////////////////////////////////////////////////////////////////
  117.  
  118. void Cls(byte Col, word Where) {
  119.   asm {
  120.     push    es           // save ES
  121.     mov     cx, 32000    // this is our loop counter.  we want to clear
  122.                          //   64000 bytes of memory, so why do we use 32000?
  123.                          //   1 word = 2 bytes, and we are moving a word at
  124.                          //   a time
  125.     mov     es, [Where]  // move address in Where to ES
  126.     xor     di, di       // zero out DI
  127.     mov     al, [Col]    // move color to AL
  128.     mov     ah, al       // move color to AH (Remember, will be moving
  129.                          //   a WORDS, so we need two copies
  130.     rep     stosw        // copy AX to Where and drecrement CX by 1
  131.                          //   until CX equals 0
  132.     pop     es           // restore ES
  133.   }
  134. }
  135.  
  136. /////////////////////////////////////////////////////////////////////////////
  137. //                                                                         //
  138. // Flip() - This copies 64000 bytes from "source" to "destination".        //
  139. //                                                                         //
  140. /////////////////////////////////////////////////////////////////////////////
  141.  
  142. void Flip(word source, word dest) {
  143.   asm {
  144.     push    ds           // save DS
  145.     mov     ax, [dest]   // copy segment of destination to AX
  146.     mov     es, ax       // set ES to point to destination
  147.     mov     ax, [source] // copy segment of source to AX
  148.     mov     ds, ax       // set DS to point to source
  149.     xor     si, si       // zero out SI
  150.     xor     di, di       // zero out DI
  151.     mov     cx, 32000    // set our counter to 32000
  152.     rep     movsw        // move source to destination by words.  decrement
  153.                          //   CX by 1 each time until CX is 0
  154.     pop     ds           // restore DS
  155.   }
  156. }
  157.  
  158. /////////////////////////////////////////////////////////////////////////////
  159. //                                                                         //
  160. // WaitRetrace() - This waits until you are in a Verticle Retrace.         //
  161. //                                                                         //
  162. /////////////////////////////////////////////////////////////////////////////
  163.  
  164. void WaitRetrace() {
  165.  
  166.   _DX = 0x03DA;
  167.  
  168.   l1: asm {
  169.     in  al,dx;
  170.     and al,0x08;
  171.     jnz l1;
  172.       }
  173.  
  174.   l2: asm {
  175.     in  al,dx;
  176.     and al,0x08;
  177.     jz  l2;
  178.       }
  179. }
  180.  
  181. //--------------------------MODE SETTING FUNCTIONS-------------------------//
  182.  
  183. /////////////////////////////////////////////////////////////////////////////
  184. //                                                                         //
  185. // SetMCGA() - This function gets you into 320x200x256 mode.               //
  186. //                                                                         //
  187. /////////////////////////////////////////////////////////////////////////////
  188.  
  189. void SetMCGA() {
  190.   _AX = 0x0013;
  191.   geninterrupt (0x10);
  192. }
  193.  
  194. /////////////////////////////////////////////////////////////////////////////
  195. //                                                                         //
  196. // SetText() - This function gets you into text mode.                      //
  197. //                                                                         //
  198. /////////////////////////////////////////////////////////////////////////////
  199.  
  200. void SetText() {
  201.   _AX = 0x0003;
  202.   geninterrupt (0x10);
  203. }
  204.  
  205.  
  206. //----------------------------PALLETTE FUNCTIONS---------------------------//
  207.  
  208. /////////////////////////////////////////////////////////////////////////////
  209. //                                                                         //
  210. // Pal() - This constructor initializes all Pal variables (R, G, and B) to //
  211. //         zero.  This ensures that all Pal objects start in a consistent  //
  212. /